package cn.com.xinjinfu.pdoulcProduct.engine;

import java.io.EOFException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.json.JSONException;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;

import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
/**
 * 请求错误回调
 * @author shi
 *
 */
public class JsonErrorListener implements ErrorListener{
    private Dialog  dialog;
    private Handler hand;
    private Context context;
    private boolean  log;
    public JsonErrorListener(Context context,Handler handler,Dialog  dg,boolean showLog){
        this.context = context;
        dialog = dg;
        hand = handler;
        log = showLog;
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        if(dialog != null)
            dialog.dismiss();
        if(hand != null){
            hand.sendEmptyMessage(0);
        }
        Throwable  th = error.getCause();
        if(log){
            if(th != null){
                if(th instanceof ConnectException || th instanceof SocketException || th instanceof UnknownHostException)
                    ToastManager.showShort(context,"网络异常");
                if(th instanceof JSONException || th instanceof EOFException){
                    ToastManager.showShort(context,"服务器错误");
                }
            }else{
                ToastManager.showShort(context,"服务器维护中o(︶︿︶)o");
            }}
        L.e("DataHander request Error! msg:"+error.getMessage()+
                "\n cause:"+error.getCause()+"\n"+" local msg:"+error.getLocalizedMessage());
    }
}
